﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_DL_Project_ManageClassNotice : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    protected string ClassUserNum = string.Empty;
    VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    TOClassNotice objClassNotice = new TOClassNotice();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            int CID = 0;
            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                ViewState["CID"] = CID.ToString();
                voClass = objClass.Load(CID);
                ClassName = voClass.Name;
                string search = GetSearch();
                BinderPG(search,1);
            }
        }
    }

    protected override void InitPage()
    {
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();
    }
    private string GetSearch()
    {
        string search = string.Empty;
        if (ViewState["CID"] != null)
        {
            search = " ClassID=" + ViewState["CID"].ToString();
            if (!string.IsNullOrEmpty(txtNewTitle.Text))
                search = string.Format("NoticeTitle like '%{0}%'", txtNewTitle.Text.Trim());
        }
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objClassNotice.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderPG(string search,int pageIndex)
    {
        string orderby = " ClassNotice_Id desc";
        DataTable dt = objClassNotice.Query(search, orderby, LMSPageSize, pageIndex);
        dt.Columns.Add("strTop");
        dt.Columns.Add("strHighLight");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["SetTop"].ToString() == "True")
                dt.Rows[i]["strTop"] = "是";
            else
                dt.Rows[i]["strTop"] = "否";
            if (dt.Rows[i]["SetHighLight"].ToString() == "True")
                dt.Rows[i]["strHighLight"] = "是";
            else
                dt.Rows[i]["strHighLight"] = "否";
        }

        rptNew.DataSource = dt;
        rptNew.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderPG(search,1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddClassNotice.aspx?CID=" + ViewState["CID"].ToString();
        Script.RedirectURL(url);
    }
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
        VOTOClassNotice vo = new VOTOClassNotice();

        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "AddClassNotice.aspx?CID=" + ViewState["CID"].ToString()+ "&NID=" + e.CommandArgument.ToString();
                Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            if (objClassNotice.Del(int.Parse(e.CommandArgument.ToString())))
            {
                string search = GetSearch();
                BinderPager(search);
                BinderPG(search, AspNetPager1.CurrentPageIndex);
            }
            else
            {
                lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
        else if (e.CommandName.ToLower() == "istop")
        {
            vo = objClassNotice.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.SetTop == true)
                vo.SetTop = false;
            else
                vo.SetTop = true;
            objClassNotice.Update(vo);
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
        }
        else if (e.CommandName.ToLower() == "ishighlight")
        {
            vo = objClassNotice.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.SetHighLight == true)
                vo.SetHighLight = false;
            else
                vo.SetHighLight = true;
            objClassNotice.Update(vo);
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
        }

    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        VOTOClassNotice vo = new VOTOClassNotice();
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NID = (Label)rptNew.Items[i].FindControl("lab_ID");
                if (cbox.Checked)
                {
                    if (objClassNotice.Del(int.Parse(lab_NID.Text)))
                    {

                    }
                    else
                    {
                        Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                    }
                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
    protected void btn_IsTop_Click(object sender, EventArgs e)
    {
        VOTOClassNotice vo = new VOTOClassNotice();
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NID = (Label)rptNew.Items[i].FindControl("lab_ID");
                if (cbox.Checked)
                {
                    vo = objClassNotice.Load(int.Parse(lab_NID.Text));
                    if (vo.SetTop == true)
                        vo.SetTop = false;
                    else
                    {
                        vo.SetTop = true;
                    }
                    objClassNotice.Update(vo);
                    
                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }

    protected void btn_IsHighLight_Click(object sender, EventArgs e)
    {
        VOTOClassNotice vo = new VOTOClassNotice();
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NID = (Label)rptNew.Items[i].FindControl("lab_ID");
                if (cbox.Checked)
                {
                    vo = objClassNotice.Load(int.Parse(lab_NID.Text));
                    if (vo.SetHighLight == true)
                        vo.SetHighLight = false;
                    else
                    {
                        vo.SetHighLight = true;
                    }
                    objClassNotice.Update(vo);

                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }

}

